package cn.ondraw.ragflow.service.impl;

import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.ragflow.pojo.request.AgentListRequest;
import cn.ondraw.ragflow.pojo.response.AgentResponse;
import cn.ondraw.ragflow.service.AgentRagFlowService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * 代理管理服务实现类
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AgentRagFlowServiceImpl implements AgentRagFlowService {

    private final PythonApiCaller pythonApiCaller;


    @Override
    public AgentResponse listAgents(AgentListRequest request) {
        try {
            String url = "/api/v1/agents?" + "page=" + request.getPage() + "&page_size=" + request.getPageSize() + "&orderby=" + request.getOrderby()
                + "&desc=" + request.getDesc() + "&name=" + request.getName() + "&id=" + request.getId();
            ResponseEntity<?> response = pythonApiCaller.callGetApi(url);
            return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, AgentResponse.class, true);
        } catch (Exception e) {
            log.error("获取Agent列表发生错误:{}", e.getMessage());
            return null;
        }
    }

}
